<template>
  <div class="role-index">
    <div class="toolbar">
      <el-button icon="el-icon-refresh" @click="refresh" :loading="loading"></el-button>
      <el-button type="danger" @click="add">添加</el-button>
    </div>

    <el-table border v-loading="loading" :data="data">
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="描述" prop="description"></el-table-column>
      <el-table-column label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag>{{scope.row.status | userStatus}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="160">
        <template slot-scope="{row}">
          <el-button size="mini" type="primary" @click="edit(row)">编辑</el-button>
          <el-button size="mini" type="danger" :disabled="row.id == 1" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :hide-on-single-page="true"
      :total="pageTotal"
      :pageSize="pageSize"
      layout="prev, pager, next, jumper, ->, total"
      @current-change="changePage"
    ></el-pagination>

    <el-dialog
      width="600px"
      top="5vh"
      :title="ruleForm.id ? '修改' : '添加'"
      :visible.sync="showDialog"
    >
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="ruleForm.description" placeholder="请输入角色描述"></el-input>
        </el-form-item>
        <el-form-item label="是否启用" prop="status">
          <el-switch v-model="ruleForm.status" :active-value="1" :inactive-value="0" :label="1"></el-switch>
        </el-form-item>
        <el-form-item label="权限" prop="rules">
          <el-tree
            :data="options"
            show-checkbox
            node-key="value"
            ref="tree"
            highlight-current
            @check-change="handleCheckChange"
          ></el-tree>
        </el-form-item>
        <div class="dialog-footer" slot="footer">
          <el-button type="primary" size="small" @click="submit('ruleForm')">提交</el-button>
        </div>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="submit('ruleForm')" :loading="loading">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import table from '../../../mixins/table'

export default {
  name: 'roleIndex',
  mixins: [table],
  data () {
    return {
      url: '/admin/roles',
      addUrl: '/admin/role',
      editUrl: '/admin/role',
      delUrl: '/admin/role',
      options: [],
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [{ required: true, message: '请输入角色描述', trigger: 'blur' }],
        rules: [{ required: true, message: '请选择权限', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    this.loadData()

    this.loadRules()
  },

  methods: {
    handleCheckChange (data, checked, indeterminate) {
      this.ruleForm.rules = this.$refs.tree.getCheckedKeys().join(',')
    },

    edit (row) {
      this.ruleForm = row
      this.showDialog = true
      setTimeout(() => {
        this.$refs.tree.setCheckedKeys(row.rules.split(','))
      }, 100)
    }
  }
}
</script>
